<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\Access\AuthorizationException as Access;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Tymon\JWTAuth\Exceptions\TokenBlacklistedException;

/**
 * Class Handler
 * @package App\Exceptions
 */
class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Session\TokenMismatchException::class,
        \Illuminate\Validation\ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception $exception
     * @return void
     * @throws Exception
     */
    public function report(Exception $exception)
    {

    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Exception $exception
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws Exception
     */
    public function render($request, Exception $exception)
    {

        if ($exception instanceof ModelNotFoundException) {
            return $this->json('没有数据', 404);
        }
        if ($exception instanceof NotFoundHttpException) {
            return $this->json('接口不存在', 404);
        }
        if ($exception instanceof MethodNotAllowedHttpException) {
            return $this->json('接口不存在', 405);
        }
        if ($exception instanceof ValidationException) {
            $errors = $exception->validator->errors();
            return $this->json($errors->first(), 200, $errors);
        }
        if ($exception instanceof Access) {
            return $this->unauthenticated($request, $exception);
        }
        if ($exception instanceof SmsValidateException){
            return $this->json('验证码错误或超时', 200);
        }
        if ($exception instanceof TokenBlacklistedException){
            return $this->json('401 请先登录', 401);
        }
        parent::report($exception);
        return parent::render($request, $exception);
    }

    /**
     * Convert an authentication exception into an unauthenticated response.
     * @param \Illuminate\Http\Request $request
     * @param AuthenticationException $exception
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    protected function unauthenticated($request,AuthenticationException $exception)
    {
        if ($exception instanceof AuthenticationException) {
            //return redirect('/front/oauth');

            return $this->json('401 请先登录', 401);
        }
        if ($exception instanceof Access) {
            return $this->json('403 没有权限', 403);
        }
        return $this->json('401 没有权限', 401);
    }

    protected function json($message, $code, $data = [])
    {
        return response()->json(['status' => -1, 'message' => $message, 'data' => $data], $code, ['Content-Type'=>'application/json; charset=UTF-8']);
    }

}
